/*******************************************************************************
* @Filename      : bsp_image.c

* @Description   : bsp_image.c

* @Version       : 1.0.0

* @Date          : 2023.12.2
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "bsp_image.h"
#include "bsp_flash.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Global variables ----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Exported functions --------------------------------------------------------*/
/*******************************************************************************
 * @Name      : gui_assets_get_image
 * @Purpose   : gui_assets_get_image
 * @Param     : None
 * @Retval    : None
 *******************************************************************************/
int gui_assets_get_image(uint32_t index, uint16_t no, gui_assets_image_t * image)
{
    uint32_t addr = GUI_GET_IMAGE_ADDR( index * PIC_LIB_INFO_SIZE ); 

    SPI_Cmd(SPI1, DISABLE);
    SPI_DMACmd(SPI1, DISABLE);
    SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Tx);
    SPI_BiDirectionalLineConfig(SPI1, SPI_Direction_Rx);
    SPI_Cmd(SPI1, ENABLE);

    SPI_FLASH_FastRead(addr, (uint8_t*)image, PIC_LIB_INFO_SIZE);
    
    SPI_Cmd(SPI1, DISABLE);
    
    // 图片序号大于图片列表个数
    if( no > image->count
    ||  image->no != index
    ||  image->h == 0
    ||  image->w == 0    
        ) 
    {
        return 1; 
    }

    // compute offset 
    
    if( no > 0)
    {
        image->address = image->address + no * image->h * image->w * GUI_LCD_PIXEL_BYTES ; 
    }
    
    image->address = GUI_GET_IMAGE_ADDR(image->address);

    return 0;
}

/* End of File ---------------------------------------------------------------*/
